package com.intech.android.demo.cms.common;


import java.util.*;

public class Util
{
    public static <T> boolean isEmpty(List<T> list)
    {
        return list == null || list.isEmpty();
    }

    public static boolean isEmpty(String str)
    {
        return str == null || str.isEmpty();
    }


    public static boolean isEmpty(Double value)
    {
        return value == null;
    }

    public static boolean isEmpty(Integer value)
    {
        return (value == null || value == 0);
    }


    public static boolean isEmpty(String[] strings)
    {
        return strings == null || strings.length < 1;
    }

    public static boolean isEmpty(Set set)
    {
        return set == null || set.isEmpty();
    }

    public static boolean isEmpty(Collection collection)
    {
        return collection == null || collection.isEmpty();
    }

    public static boolean isEmpty(List... lists)
    {
        for (List list : lists)
        {
            if (Util.isEmpty(list))
            {
                return true;
            }
        }
        return false;
    }

    public static String trim(String name)
    {
        if (isEmpty(name))
        {
            return name;
        }
        return name.trim();
    }

    public static <T> List<T> uniqueOrderAdd(List<T> sourceList, List<T> list)
    {
        for (T source : sourceList)
        {
            uniqueOrderAdd(source, list);
        }
        return list;
    }

    public static <T> List<T> uniqueOrderAdd(Set<T> sourceList, List<T> list)
    {
        for (T source : sourceList)
        {
            uniqueOrderAdd(source, list);
        }
        return list;
    }


    public static <T> List<T> uniqueOrderAdd(T element, List<T> list)
    {
        Set<T> buffer = null;
        if (list != null)
        {
            buffer = new TreeSet<T>(list);
            list.clear();
            buffer.add(element);
            list.addAll(buffer);
        }
        else
        {
            buffer = new TreeSet<T>();
            buffer.add(element);
            list = new ArrayList<T>(buffer);
        }
        return list;
    }

    public static <T> boolean uniqueOrderCheckedAdd(T element, List<T> list)
    {
        Set<T> buffer = new TreeSet<T>(list);
        list.clear();
        boolean added = buffer.add(element);
        list.addAll(buffer);
        return added;
    }

    public static <T> List<T> uniqueAdd(T element, List<T> list)
    {
        Set<T> buffer = null;
        if (list != null)
        {
            buffer = new HashSet<T>(list);
            list.clear();
            buffer.add(element);
            list.addAll(buffer);
        }
        else
        {
            buffer = new HashSet<T>();
            buffer.add(element);
            list = new ArrayList<T>(buffer);
        }
        return list;
    }

    public static <T> List<T> uniqueAddSameOrder(T element, List<T> list, int position)
    {
        if (list == null)
        {
            list = new ArrayList<T>();
            list.add(position, element);
        }
        else
        {
            if (list.contains(element))
            {
                return list;
            }
            list.add(position, element);
        }
        return list;
    }

    public static <T> boolean uniqueAddSameOrder(T element, List<T> list)
    {
        boolean added = false;
        if (list == null)
        {
            list = new ArrayList<T>();
            added = list.add(element);
        }
        else
        {
            if (list.contains(element))
            {
                return added;
            }
            added = list.add(element);
        }
        return added;
    }


    public static <T> List<T> uniqueFilter(List<T> list)
    {
        Set<T> buffer = new HashSet<T>(list);
        list.clear();
        list.addAll(buffer);
        return list;
    }
}
